package com.myown.filter;

import com.myown.constant.ConstantInfo;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


/**
 * @author lincky
 * @version v1.0.0
 */

public class AccountFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        HttpSession session = httpRequest.getSession();
        if (session.getAttribute(ConstantInfo.Account.HTTP_GLOBAL_ACCOUNT) == null) {
            if (session.getAttribute(ConstantInfo.Account.THE_OTHER_LOGONED) != null) {
                session.setAttribute("logoned", "异地已登录");
            } else {
                session.setAttribute("logoned", "未登录");
            }
//            httpRequest.getRequestDispatcher(ConstantInfo.Path.RELATIVE_PATH_LOGIN)
//                    .forward(request, response);
            httpResponse.sendRedirect(ConstantInfo.Path.RELATIVE_PATH_LOGIN);
        } else {
            session.setAttribute("logoned", "登录成功");
        }
        System.out.println("request :" + session.getAttribute("logoned"));
        chain.doFilter(request, response);
    }
    @Override
    public void destroy() {

    }
}
